package com.example.kotlinstudy.s6

import org.junit.Test

class Test_flatMap_filter {
    //map
    @Test
    fun test1() {

        //原始集合没有改变， 返回的时一个新集合，这样变量就不会变来变去
        val animals = listOf("a", "b", "c", "d", "e")
        val babies = animals
            .map { "A baby $it" }
            .map { "~~A baby $it" }

        println(animals)
        println(babies)

        val listLength = animals.map { it.length }
        println(listLength)


        //flatMap
        //可以将多个集合中的元素合并
        val result = listOf(listOf(1, 2, 3), listOf(1, 2, 3)).flatMap { it }
        println(result)
    }


    @Test //filter 过滤
    fun test2() {
        val listOf = listOf("Jack", "Jimmy", "Rose", "Tom").filter { it.contains("J") }
        println(listOf)
    }
}